package com.java.dao.impl;


import com.java.dao.BasicDao;
import com.java.dao.UsersDao;
import com.java.domain.Users;
import com.java.utils.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.management.Query;
import java.sql.SQLException;
import java.util.List;

public class UsersDaoImpl extends BasicDao<Users> implements UsersDao {
    private QueryRunner runner =new QueryRunner(DruidUtils.getDataSource());
     String sql;
    @Override
    public int addUsers(Users users) {
        sql="insert into users(uName,pwd)values(?,?)";
        return update(sql,users.getuName(),users.getPwd());
    }

    @Override
    public Users queryUser(Users users) {
        sql="select uName,pwd from users where uName=? and pwd=?";
        return querySingle(sql,Users.class,users.getuName(),users.getPwd());
    }

    //查询用户
    @Override
    public List<Users> Selectuser() {
        sql="select * from users";
        try {
            return runner.query(sql,new BeanListHandler<>(Users.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //删除用户
    @Override
    public int delUsers(String uName) {
        sql="delete from users where uName =?";
        try {
            return  runner.update(sql,uName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

}
